package state;

import state.component.Style;

public class AlertMenu extends GameForm {
  private Image[] iconImages;
  private String[] menu;
  private AlertListener alertListener;
  private int selectedIndex;
  private int alertId;
  
  private Style style = new Style();
  
  private static AlertMenu singleton;

  private static AlertMenu instance() {
    if (singleton == null) {
      singleton = new AlertMenu();
    }
    return singleton;
  }
  
  private void init(AlertListener alertListener, Image[] iconImages, String[] menu, int alertId) {
    this.alertListener = alertListener;
    this.iconImages = iconImages;
    this.menu = menu;
    this.alertId = alertId;
    selectedIndex = 0;
    resize(10, menu.length * 30);
  }
  
  private void resize(int width, int heigth) {
    setSize(width, heigth);
    setPosition(GameConstants.SCREEN_WIDTH / 2 - width / 2, GameConstants.SCREEN_HEIGHT / 2 - heigth / 2);
  }
  
  public static void setStyle(Style style) {
    AlertMenu alertMenu = AlertMenu.instance();
    alertMenu.style = style;
  }
  
  public static void showAlert(AlertListener alertListener, Image[] iconImages, String[] menu, int alertId) {
    AlertMenu alertMenu = AlertMenu.instance();
    alertMenu.init(alertListener, iconImages, menu, alertId);
    GameGlobal.alert.showCustomUIAlert(alertMenu, alertId);
  }
  
  public void draw(Graphics g) {
    int maxWidth = 0;
    int maxHeight = 0;
    int maxImageWidth = 0;
    
    if (style.isFillBackGround()) {
      g.setColor(style.getBackgroundColor());
      g.fillRect(getRealX(), getRealY(), getWidth(), getHeight());
    }
    
    for (int i = 0; i < menu.length; i++) {
      int rowHeight = 4 + ((iconImages == null || iconImages.length <= i || iconImages[i] == null) ? style.getFont().getHeight() : Math.max(style.getFont().getHeight(), iconImages[i].getHeight()));
      maxImageWidth = (iconImages == null || iconImages.length <= i || iconImages[i] == null) ? maxImageWidth : Math.max(maxImageWidth, iconImages[i].getWidth());
      int rowWidth = (iconImages == null || iconImages.length <= i || iconImages[i] == null) ? style.getFont().stringWidth(menu[i]) : style.getFont().stringWidth(menu[i]) + maxImageWidth + 10;
      
      maxHeight += rowHeight;
      if (rowWidth > maxWidth) {
        maxWidth = rowWidth;
      }
      
      int textColor = style.getForceGroundColor();
      if (i == selectedIndex) {
        g.setColor(style.getFocusBgColor());
        g.fillRect(getRealX(), getRealY() + maxHeight - rowHeight, maxWidth, rowHeight);
        
        textColor = style.getFocusFgColor();
      }
      
      if (iconImages != null && iconImages.length > i && iconImages[i] != null) {
        g.drawImage(iconImages[i], getRealX() + 2, getRealY() + maxHeight - rowHeight + 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      style.getFont().drawString(g, menu[i], textColor, getRealX() + maxImageWidth + 7, getRealY() + maxHeight - rowHeight + 1, GameConstants.TOP_LEFT_ANCHOR);
    }
    
    if (style.isHasBorder()) {
      g.setColor(style.getBorderColor());
      g.drawRect(getRealX(), getRealY(), getWidth() - 1, getHeight() - 1);
    }
    resize(maxWidth, maxHeight);
  }
  
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.UP:
      selectedIndex = (--selectedIndex < 0) ? 0 : selectedIndex;
      break;
    case Key.DOWN:
      selectedIndex = (++selectedIndex > menu.length - 1) ? menu.length - 1 : selectedIndex;
      break;
    case Key.FIRE:
      GameGlobal.systemCanvas.hideAlert();
      if (alertListener != null) {
        alertListener.alertEventPerform(Alert.CUSTOM_UI_TYPE, selectedIndex, alertId);
      }
      destroy();
      break;
    }
  }
  
  public void destroy() {
    iconImages = null;
    menu = null;
    alertListener = null;
  }
}
